import MsuiFormItemModel from "./msui-form-item-model";
import { reactive } from "vue";
class MsuiFormModel {
  constructor(model) {
    this.bind = {};
    this.vOn = {};
    this.formModel = {};
    this.buttonOptions = {};
    this.init(model);
  }
  init(model) {
    let models = {};
    for (let key in model) {
      this.formModel[key] = new MsuiFormItemModel({
        label: model[key],
        name: key,
        parent: this,
      });
      models[key] = null;
    }
    this.setBind({ model: reactive(models) });
  }
  setBind(bind) {
    this.bind = { ...bind, ...this.bind };
    return this;
  }
  setEvent(event) {
    this.vOn = event;
    return this;
  }
  setButtonOptions(options) {
    this.buttonOptions = options;
    return this;
  }
  //批量设置表单验证条件
  addRules(ruleset, keyList = []) {
    if (keyList.length > 0) {
      keyList.forEach((key) => {
        this.formModel[key].addRules(ruleset);
      });
      return this;
    }
    for (let key in this.formModel) {
      this.formModel[key].addRules(ruleset);
    }
    return this;
  }
  //设置表单必填项
  isRequired(keyList) {
    keyList.forEach((key) => {
      this.formModel[key].isRequired();
    });
    return this;
  }
  setData(data) {
    for(let key in data) {
      this.bind.model[key] = data[key]
    }
    return this
  }
  getData() {
    return this.bind.model
  }
}

export default MsuiFormModel;
